<?php
/**
 * @package   auto_occasion
 * @subpackage 
 * @author    CMS
 * @copyright 2011
 * @license    All rights reserved
 */


/**
 * Classe permettant de gerer les pays
 *
 */
class paysSrv {

    /**
     * Liste des pays
     * 
     * @return array
     */
    public static function liste () {

        $sql = " SELECT * FROM ao_ip_to_country GROUP BY COUNTRY_CODE ORDER BY COUNTRY_NAME";
        $jdb = jDb::getDbWidget();
        $ret = $jdb->fetchAll($sql);

        return $ret;
    }

    /**
     * Liste des pays interdits
     * 
     * @return array
     */
    public static function interdit () {

        $sql = " SELECT code FROM ao_pays_interdit";
        $cnx = jDb::getConnection();
        $rs  = $cnx->query($sql);
        $res = array();
        while ($row = $rs->fetch()) {
            $res[] = $row->code;
        }

         return $res;
    }

    /**
     * Liste des pays interdits selon le code pays
     * 
     * @param $code
     * @return array
     */
    public static function interditByCode ($code) {

        echo $sql = " SELECT COUNTRY_NAME FROM ao_ip_to_country WHERE COUNTRY_CODE IN ($code)";
        $cnx = jDb::getConnection();
        $rs  = $cnx->query($sql);
        $res = array();
        while ($row = $rs->fetch()) {
            $res[] = $row->COUNTRY_NAME;
        }

         return $res;
    }


    /**
     * Enregistre le pays interdits
     * 
     * @param array $pays
     * @return array
     */
    public static function save ($pays) {

        $cnx = jDb::getConnection();
        try {
            $truncate = " TRUNCATE TABLE ao_pays_interdit";
            $cnx->exec($truncate);

        }  catch(exception $ex) {
            die($ex->getMessage());
        }


        $sql = " INSERT INTO ao_pays_interdit (code) VALUES ";

        foreach ($pays as $k=>$item) {
            $sep = (!$k)? "": ",";
            $sql .= $sep."('".$item."')";
        }

        try {
            $cnx->exec($sql);
        } catch(exception $e) {
            die($e->getMessage());
        }
    }

    
}